package com.hd.trans.dialog

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.CheckBox
import android.widget.TextView
import androidx.annotation.LayoutRes
import androidx.databinding.ViewDataBinding
import com.hd.trans.R
import com.hd.trans.framework.dialog.CstDialog

/**
 * 自定义的确认弹窗
 */
open class CustomConfirmDialog(
    context: Context,
    @LayoutRes layoutId: Int
) : CstDialog<ViewDataBinding>(
    context,
    LayoutInflater.from(context).inflate(layoutId, null),
    false
){

    var leftClick : (()->Unit)? = null
    var rightClick : (()->Unit)? = null
    var closeClick : (()->Unit)? = null
    var checkBoxClick : ((Boolean)->Unit)? = null

    var titleText:String
        get(){
            return contentView.findViewById<TextView>(R.id.tv_title)?.text?.toString()?:""
        }
        set(value) {
            contentView.findViewById<TextView>(R.id.tv_title)?.text = value
        }

    var contentText:String
        get() {
            return contentView.findViewById<TextView>(R.id.tv_content)?.text?.toString()?:""
        }
        set(value) {
            contentView.findViewById<TextView>(R.id.tv_content)?.let {
                if (value.isEmpty()){
                    it.visibility = View.GONE
                }
                else{
                    it.visibility = View.VISIBLE
                    it.text = value
                }
            }
        }

    var leftText:String
        get() {
            return contentView.findViewById<TextView>(R.id.btn_left)?.text?.toString()?:""
        }
        set(value) {
            contentView.findViewById<TextView>(R.id.btn_left)?.let {
                it.text = value
            }
        }

    var rightText:String
        get() {
            return contentView.findViewById<TextView>(R.id.btn_right)?.text?.toString()?:""
        }
        set(value) {
            contentView.findViewById<TextView>(R.id.btn_right)?.let {
                it.text = value
            }
        }

    private val checkBox: CheckBox? by lazy {
        contentView.findViewById(R.id.check_box)
    }

    init {
        val root = contentView

        root.findViewById<View>(R.id.btn_left)?.setOnClickListener {
            dismiss()
            leftClick?.invoke()
        }

        root.findViewById<View>(R.id.btn_right)?.setOnClickListener {
            dismiss()
            rightClick?.invoke()
        }

        root.findViewById<View>(R.id.btn_close)?.setOnClickListener {
            dismiss()
            closeClick?.invoke()
        }

        root.findViewById<View>(R.id.ll_check_box)?.setOnClickListener{
            checkBox?.let {
                val checked = !it.isChecked
                it.isChecked = checked
                checkBoxClick?.invoke(checked)
            }
        }

    }

    fun setOnLeftClickListener(click:Runnable){
        leftClick = {
            click.run()
        }
    }

    fun setOnRightClickListener(click:Runnable){
        rightClick = {
            click.run()
        }
    }

}
